package com.example.simple_android.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.view.View;

import com.example.simple_android.annotation.Layout;
import com.example.simple_android.annotation.impl.InitViewImpl;
import com.example.simple_android.annotation.impl.InjectionImpl;
import com.example.simple_android.annotation.impl.InjectionMarkImpl;
import com.example.simple_android.annotation.impl.ReceiverImpl;
import com.example.simple_android.controller.impl.ViewController;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * 继承此类，创建自定义的视图窗口
 */
public abstract class BaseActivity extends AppCompatActivity {

    private Set<BroadcastReceiver> broadcastReceivers;

    public BaseActivity() {
        super();
        broadcastReceivers = new HashSet<>();
        InjectionMarkImpl.injectionMarkClass(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 使用Layout注解创建指定视图
        Layout declaredAnnotation = this.getClass().getDeclaredAnnotation(Layout.class);
        if (declaredAnnotation != null) {
            int layoutId = declaredAnnotation.layout();
            setContentView(layoutId);
        }
        // 自我扫描，尝试寻找被Injection注解标记的Field，并且尝试为其赋值，从InjectionManager中寻找
        InjectionImpl.injection(this);
        // 自我广播扫描并且注册匿名内部类广播对象，注册非标准广播对象
        Set<BroadcastReceiver> broadcastReceivers = ReceiverImpl.initFieldReceiver(this, this);
        if (broadcastReceivers != null) {
            this.broadcastReceivers.addAll(broadcastReceivers);
        }
        // 自我广播扫描，尝试寻找作为成员内部类的标准广播以及非标准广播对象，并且尝试将其注册到指定上下文中
        broadcastReceivers = ReceiverImpl.initInnerClassReceiver(this, this);
        if (broadcastReceivers != null) {
            this.broadcastReceivers.addAll(broadcastReceivers);
        }
        // 初始化视图组件
        initView();
        // 注册视图的点击事件，此方法用开发者重写
        initClick();
        // 初始化Controller
        initController();
    }

    protected abstract void initController();

    protected abstract void initClick();

    protected void initView() {
        // 从当前对象中，寻扎送findViewById方法
        Method findViewById = getMethod(this.getClass(), "findViewById");
        if (findViewById != null) {
            // 将被InitView注解标记的Field，初始化，使用findViewById方法
            InitViewImpl.initView(this, findViewById);
        }
    }

    private Method getMethod(Class<?> cls, String methodName) {
        Method findViewById;
        try {
            findViewById = cls.getSuperclass().getDeclaredMethod(methodName, int.class);
        } catch (NoSuchMethodException e) {
            if (cls.getSuperclass().equals(Object.class)) {
                return null;
            }
            findViewById = getMethod(cls.getSuperclass(), methodName);
        }
        return findViewById;
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 将上下文中注册的所有广播，注销
        if (broadcastReceivers != null) {
            broadcastReceivers.removeIf(broadcastReceiver -> {
                if (broadcastReceiver != null) {
                    unregisterReceiver(broadcastReceiver);
                }
                return true;
            });
        }
    }

    protected View injectionController(View view, ViewController controller) {
        controller.init(view);
        controller.initValue();
        controller.initElementClick();
        controller.initElementLongClick();
        controller.initElementTouchClick();
        return view;
    }

}